home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / FileDialog.java < prev    next >
Text File  |  1998-09-22  |  8KB  |  263 lines

  1. /*
  2.  * @(#)FileDialog.java    1.32 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14. package java.awt;
  15.  
  16. import java.awt.peer.FileDialogPeer;
  17. import java.io.FilenameFilter;
  18.  
  19. /**
  20.  * The <code>FileDialog</code> class displays a dialog window 
  21.  * from which the user can select a file. 
  22.  * <p>
  23.  * Since it is a modal dialog, when the application calls 
  24.  * its <code>show</code> method to display the dialog, 
  25.  * it blocks the rest of the application until the user has 
  26.  * chosen a file. 
  27.  *
  28.  * @see Window#show
  29.  *
  30.  * @version     1.32, 07/01/98
  31.  * @author     Sami Shaio
  32.  * @author     Arthur van Hoff
  33.  * @since       JDK1.0
  34.  */
  35. public class FileDialog extends Dialog {
  36.     
  37.     /**
  38.      * This constant value indicates that the purpose of the file  
  39.      * dialog window is to locate a file from which to read. 
  40.      * @since    JDK1.0
  41.      */
  42.     public static final int LOAD = 0;
  43.  
  44.     /**
  45.      * This constant value indicates that the purpose of the file  
  46.      * dialog window is to locate a file to which to write. 
  47.      * @since    JDK1.0
  48.      */
  49.     public static final int SAVE = 1;
  50.  
  51.     int mode;
  52.     String dir;
  53.     String file;
  54.     FilenameFilter filter;
  55.  
  56.     private static final String base = "filedlg";
  57.     private static int nameCounter = 0;
  58.  
  59.     /*
  60.      * JDK 1.1 serialVersionUID 
  61.      */
  62.      private static final long serialVersionUID = 5035145889651310422L;
  63.  
  64.     /**
  65.      * Creates a file dialog for loading a file.  The title of the
  66.      * file dialog is initially empty.
  67.      * @param parent the owner of the dialog
  68.      * @since JDK1.1
  69.      */
  70.     public FileDialog(Frame parent) {
  71.     this(parent, "", LOAD);
  72.     }
  73.  
  74.     /**
  75.      * Creates a file dialog window with the specified title for loading 
  76.      * a file. The files shown are those in the current directory. 
  77.      * @param     parent   the owner of the dialog.
  78.      * @param     title    the title of the dialog.
  79.      * @since     JDK1.0
  80.      */
  81.     public FileDialog(Frame parent, String title) {
  82.     this(parent, title, LOAD);
  83.     }
  84.  
  85.     /**
  86.      * Creates a file dialog window with the specified title for loading 
  87.      * or saving a file. 
  88.      * <p>
  89.      * If the value of <code>mode</code> is <code>LOAD</code>, then the 
  90.      * file dialog is finding a file to read. If the value of 
  91.      * <code>mode</code> is <code>SAVE</code>, the file dialog is finding 
  92.      * a place to write a file. 
  93.      * @param     parent   the owner of the dialog.
  94.      * @param     title   the title of the dialog.
  95.      * @param     mode   the mode of the dialog.
  96.      * @see       java.awt.FileDialog#LOAD
  97.      * @see       java.awt.FileDialog#SAVE
  98.      * @since     JDK1.0
  99.      */
  100.     public FileDialog(Frame parent, String title, int mode) {
  101.     super(parent, title, true);
  102.     this.mode = mode;
  103.     setLayout(null);
  104.     }
  105.  
  106.     /**
  107.      * Construct a name for this component.  Called by getName() when the
  108.      * name is null.
  109.      */
  110.     String constructComponentName() {
  111.         return base + nameCounter++;
  112.     }
  113.  
  114.     /**
  115.      * Creates the file dialog's peer.  The peer allows us to change the look
  116.      * of the file dialog without changing its functionality.
  117.      */
  118.     public void addNotify() {
  119.         synchronized(getTreeLock()) {
  120.         if (peer == null)
  121.             peer = getToolkit().createFileDialog(this);
  122.         super.addNotify();
  123.         }
  124.     }
  125.  
  126.     /**
  127.      * Indicates whether this file dialog box is for loading from a file 
  128.      * or for saving to a file. 
  129.      * @return   the mode of this file dialog window, either 
  130.      *               <code>FileDialog.LOAD</code> or 
  131.      *               <code>FileDialog.SAVE</code>.
  132.      * @see      java.awt.FileDialog#LOAD
  133.      * @see      java.awt.FileDialog#SAVE
  134.      * @see      java.awt.FileDialog#setMode
  135.      * @since    JDK1.0
  136.      */
  137.     public int getMode() {
  138.     return mode;
  139.     }
  140.  
  141.     /**
  142.      * Sets the mode of the file dialog.
  143.      * @param      mode  the mode for this file dialog, either 
  144.      *                 <code>FileDialog.LOAD</code> or 
  145.      *                 <code>FileDialog.SAVE</code>.
  146.      * @see        java.awt.FileDialog#LOAD
  147.      * @see        java.awt.FileDialog#SAVE
  148.      * @see        java.awt.FileDialog#getMode
  149.      * @exception  IllegalArgumentException if an illegal file 
  150.      *                 dialog mode is used.
  151.      * @since      JDK1.1
  152.      */
  153.     public void setMode(int mode) {
  154.     switch (mode) {
  155.       case LOAD:
  156.       case SAVE:
  157.         this.mode = mode;
  158.         break;
  159.       default:
  160.         throw new IllegalArgumentException("illegal file dialog mode");
  161.     }
  162.     }
  163.  
  164.     /**
  165.      * Gets the directory of this file dialog.
  166.      * @return    the directory of this file dialog.
  167.      * @see       java.awt.FileDialog#setDirectory
  168.      * @since     JDK1.0
  169.      */
  170.     public String getDirectory() {
  171.     return dir;
  172.     }
  173.  
  174.     /**
  175.      * Sets the directory of this file dialog window to be the  
  176.      * specified directory. 
  177.      * @param     dir   the specific directory.
  178.      * @see       java.awt.FileDialog#getDirectory
  179.      * @since     JDK1.0
  180.      */
  181.     public void setDirectory(String dir) {
  182.     this.dir = dir;
  183.     FileDialogPeer peer = (FileDialogPeer)this.peer;
  184.     if (peer != null) {
  185.         peer.setDirectory(dir);
  186.     }
  187.     }
  188.  
  189.     /**
  190.      * Gets the selected file of this file dialog.
  191.      * @return    the currently selected file of this file dialog window, 
  192.      *                or <code>null</code> if none is selected.
  193.      * @see       java.awt.FileDialog#setFile
  194.      * @since     JDK1.0
  195.      */
  196.     public String getFile() {
  197.     return file;
  198.     }
  199.  
  200.     /**
  201.      * Sets the selected file for this file dialog window to be the 
  202.      * specified file. This file becomes the default file if it is set 
  203.      * before the file dialog window is first shown. 
  204.      * @param    file   the file being set.
  205.      * @see      java.awt.FileDialog#getFile
  206.      * @since    JDK1.0
  207.      */
  208.     public void setFile(String file) {
  209.     this.file = file;
  210.     FileDialogPeer peer = (FileDialogPeer)this.peer;
  211.     if (peer != null) {
  212.         peer.setFile(file);
  213.     }
  214.     }
  215.     
  216.     /**
  217.      * Determines this file dialog's filename filter. A filename filter 
  218.      * allows the user to specify which files appear in the file dialog 
  219.      * window. 
  220.      * @return    this file dialog's filename filter.
  221.      * @see       java.io.FilenameFilter
  222.      * @see       java.awt.FileDialog#setFilenameFilter
  223.      * @since     JDK1.0
  224.      */
  225.     public FilenameFilter getFilenameFilter() {
  226.     return filter;
  227.     }
  228.  
  229.     /**
  230.      * Sets the filename filter for this file dialog window to the 
  231.      * specified filter. 
  232.      * @param   filter   the specified filter.
  233.      * @see     java.io.FilenameFilter
  234.      * @see     java.awt.FileDialog#getFilenameFilter
  235.      * @since   JDK1.0
  236.      */
  237.     public synchronized void setFilenameFilter(FilenameFilter filter) {
  238.     this.filter = filter;
  239.     FileDialogPeer peer = (FileDialogPeer)this.peer;
  240.     if (peer != null) {
  241.         peer.setFilenameFilter(filter);
  242.     }
  243.     }
  244.  
  245.     /**
  246.      * Returns the parameter string representing the state of this file 
  247.      * dialog window. This string is useful for debugging. 
  248.      * @return  the parameter string of this file dialog window.
  249.      * @since   JDK1.0
  250.      */
  251.     protected String paramString() {
  252.     String str = super.paramString();
  253.     if (dir != null) {
  254.         str += ",dir= " + dir;
  255.     }
  256.     return str + ((mode == LOAD) ? ",load" : ",save");
  257.     }
  258.  
  259.     boolean postsOldMouseEvents() {
  260.         return false;
  261.     }
  262. }
  263.